﻿using System;
using System.Linq;
using IterationLife.ContentResources.Models;
using IterationLife.ContentResources.Repositories;
using IterationLife.Datas;
using IterationLife.Extensions.Dtos;
using IterationLife.Mobile.AppServices.Dtos.Common;
using IterationLife.UserProfiles.Repositories;
using Util;
using Util.Domains.Contexts;
using Util.Exceptions;

namespace IterationLife.Mobile.AppServices.Services.Common.Implements
{
    /// <summary>
    /// 内容资源评论服务
    /// </summary>
    public class ContentResourceCommentService : IContentResourceCommentService
    {
        /// <summary>
        /// 应用程序工作单元
        /// </summary>
        private readonly IApplicationUnitOfWork _unitOfWork;

        /// <summary>
        /// 内容资源评论仓储
        /// </summary>
        private readonly IContentResourceCommentRepository _commetRepository;

        /// <summary>
        /// 用户资料仓储
        /// </summary>
        private readonly IUserProfileRepository _userProfileRepository;

        /// <summary>
        /// 初始内容资源评论服务
        /// </summary>
        /// <param name="unitOfWork">应用程序工作单元</param>
        /// <param name="commetRepository">内容资源评论仓储</param>
        /// <param name="userProfileRepository">用户资料仓储</param>
        public ContentResourceCommentService(
            IApplicationUnitOfWork unitOfWork,
            IContentResourceCommentRepository commetRepository,
            IUserProfileRepository userProfileRepository
            )
        {
            _unitOfWork = unitOfWork;
            _commetRepository = commetRepository;
            _userProfileRepository = userProfileRepository;
        }

        /// <summary>
        /// 评论分页列表
        /// </summary>
        /// <param name="resourceId">内容资源编号</param>
        /// <param name="page">页数</param>
        /// <param name="size">条数</param>
        public PagedData<ContentResourceCommentOutPut> PagedList(Guid resourceId, int page, int size)
        {
            var entities = _commetRepository.Find()
                           .Where(t => t.ContentResourceId == resourceId && t.Level == 1 && t.Enabled)
                           .OrderBy(t => t.SortId)
                           .Take(size * page)
                           .Skip(size * (page - 1))
                           .ToList();
            return new PagedData<ContentResourceCommentOutPut>(entities.Select(t => t.ToNewsCommentOutPut()), page, size, _commetRepository.Find().Count(t => t.Level == 1 && t.Enabled));
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="resourceId">内容资源编号</param>
        /// <param name="content">内容</param>
        public void Add(Guid resourceId, string content)
        {
            var entity = GetEntity(resourceId, content);
            entity.Init();
            entity.InitPath();
            _commetRepository.Add(entity);
            _unitOfWork.Commit();
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="resourceId">内容资源编号</param>
        /// <param name="content">内容</param>
        private ContentResourceComment GetEntity(Guid resourceId, string content)
        {
            var userEntity = _userProfileRepository.Find(ApplicationContext.User.UserId.ToGuid());
            if (userEntity == null)
                throw new Warning("用户编号无效");
            return new ContentResourceComment
            {
                ContentResourceId = resourceId,
                Content = content,
                UserId = userEntity.Id,
                UserName = userEntity.NickName.IsEmpty() ? "匿名" : userEntity.NickName,
                Enabled = true,
                SortId = _commetRepository.Find().Where(t => t.ContentResourceId == resourceId && t.Level == 1 && t.Enabled).Max(t => t.SortId).SafeValue() + 1,
                CreationTime = DateTime.Now,
            };
        }

    }
}
